package somar_test_hello.parser.rselV1_4a;
import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RSELSaxEvent extends DefaultHandler{
	private Stack<EntryElement> stack;
	private EntryElement node = null;
	private EntryElement rootNode;

	public RSELSaxEvent(){
		rootNode = null;
		stack  = new Stack<EntryElement>();
	}

	public void startDocument() throws SAXException{
	}

	public void endDocument() throws SAXException{
		if(stack.size() == 0){
			System.out.println("Accept\n");
		}else{
			System.out.println("Error\n");
		}
	}

	public void startElement(String uri, String localName, String rawName, Attributes attr) throws SAXException{
		if(rawName.equals(EntryElement.PROCESSELMTNAME)) {
			node = new ProcessElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.CLASSLIBELMTNAME)) {
			node = new ClasslibElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.CLASSELMTNAME)) {
			node = new ClassElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.VARIABLEELMTNAME)) {
			node = new VariableElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.RSELELMTNAME)) {
			node = new RSELElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.INVOKEELMTNAME)) {
			node = new InvokeElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.DOCUMENTATIONELMTNAME)) {
			node = new DocumentationElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.TOELMTNAME)) {
			node = new ToElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.FROMELMTNAME)) {
			node = new FromElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.PARAMETERELMTNAME)) {
			node = new ParameterElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.RETURNELMTNAME)) {
			node = new ReturnElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.VARIABLESELMTNAME)) {
			node = new VariablesElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.IMPORTELMTNAME)) {
			node = new ImportElement(rawName, attr);
		}
		else if(rawName.equals(EntryElement.ASSIGNELMTNAME)) {
			node = new AssignElement(rawName, attr);
		}

		if(rootNode == null)
			rootNode = node;

		stack.push(node);
	}

	public void endElement(String uri, String localName, String rawName) throws SAXException{
		EntryElement comp = stack.pop();

		if(comp.getQName().equals(rawName) && stack.size()>0)
			stack.peek().addChild(comp);
	}

	public void characters(char ch[], int start, int length) throws SAXException{
		String characters = new String(ch, start, length);
		stack.peek().setCharacters(characters);
	}

	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException{
		characters(ch, start, length);
	}

	public EntryElement getRootElement(){
		return rootNode;
	}
}